package org.anddev.andengine.extension.svg;
/**
 * @author Eric Depta
 * @since 01:14:00 - 28.02.2011
 */
import java.util.ArrayList;
import java.util.List;

import org.anddev.andengine.extension.svg.node.CircleNode;
import org.anddev.andengine.extension.svg.node.EllipseNode;
import org.anddev.andengine.extension.svg.node.LineNode;
import org.anddev.andengine.extension.svg.node.PathNode;
import org.anddev.andengine.extension.svg.node.PolyLineNode;
import org.anddev.andengine.extension.svg.node.PolygonNode;
import org.anddev.andengine.extension.svg.node.RectNode;
import org.anddev.andengine.extension.svg.node.TransformableNode;
import org.anddev.andengine.extension.svg.util.constants.SVGConstants;
import org.anddev.andengine.extension.svg.util.transform.TransformDoc;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SVGParser extends DefaultHandler implements SVGConstants{
	private final SVGDoc mSVGDoc;
	private final List<TransformDoc> mTransformStack = new ArrayList<TransformDoc>();

	public SVGParser(final SVGDoc pSVGDoc) {
		this.mSVGDoc = pSVGDoc;
		this.mTransformStack.add(new TransformDoc());
	}
	
	@Override
	public void startElement(final String pUri, final String pLocalName, final String pQualifiedName, final Attributes pAttributes) throws SAXException {
		if(pLocalName.equals(TAG_RECT)){
			new RectNode(this.mSVGDoc).create(pAttributes, this.getCurrentTransform());
		}
		if(pLocalName.equals(TAG_CIRCLE)){
			new CircleNode(this.mSVGDoc).create(pAttributes, this.getCurrentTransform());
		}
		if(pLocalName.equals(TAG_LINE)){
			new LineNode(this.mSVGDoc).create(pAttributes, this.getCurrentTransform());
		}
		if(pLocalName.equals(TAG_PATH)){
			new PathNode(this.mSVGDoc).create(pAttributes, this.getCurrentTransform());
		}
		if(pLocalName.equals(TAG_POLYLINE)){
			new PolyLineNode(this.mSVGDoc).create(pAttributes, this.getCurrentTransform());
		}
		if(pLocalName.equals(TAG_POLYGON)){
			new PolygonNode(this.mSVGDoc).create(pAttributes, this.getCurrentTransform());
		}
		if(pLocalName.equals(TAG_ELLIPSE)){
			new EllipseNode(this.mSVGDoc).create(pAttributes, this.getCurrentTransform());
		}
		//--
		if(pLocalName.equals(TAG_GROUP)){
			final TransformableNode group = new TransformableNode(this.mSVGDoc);
			group.create(pAttributes, this.getCurrentTransform());
			mTransformStack.add(group.getTransformation());
		}
	}

	@Override
	public void characters(final char[] pCharacters, final int pStart, final int pLength) throws SAXException {
		
	}

	@Override
	public void endElement(final String pUri, final String pLocalName, final String pQualifiedName) throws SAXException {
		if(pLocalName.equals(TAG_GROUP)){
			this.mTransformStack.remove(this.mTransformStack.size()-1);
		}
	}
	
	public SVGDoc getSCGDoc(){
		return this.mSVGDoc;
	}
	
	private TransformDoc getCurrentTransform(){
		return this.mTransformStack.get(this.mTransformStack.size()-1);
	}
	
}
